using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using Decav.Security;
using Decav.Gatsb;
using Decav.Gatsb.Application;
using Decav.Gatsb.Web;
using Decav.Reality;

public partial class LoginBox : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        loginErrorLabel.Visible = false;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "SetPrimaryFocus",
                    string.Format("document.getElementById(\"{0}\").focus();", loginTextBox.ClientID), true);
    }

    protected void Login_OnClick(object sender, EventArgs e)
    {
        if (loginTextBox.Text.Length == 0 || passwordTextBox.Text.Length == 0)
        {
            loginErrorLabel.InnerText = "Please provide your email address and password to login.";
            loginErrorLabel.Visible = true;
            return;
        }

        EmailAddress addr = EmailAddress.Empty;
        HashedPassword password = HashedPassword.HashPassword(passwordTextBox.Text);

        try
        {
            addr = new EmailAddress(loginTextBox.Text);
        }
        catch
        {
            // Swallow the exception and show an error...
            loginErrorLabel.InnerText = "The email address you specified is not valid.";
            loginErrorLabel.Visible = true;
            return;
        }

        UserLoginResult result = GatsbWebApplication.Current.AuthenticateUser(addr, password, true);

        if (!result.Authenticated)
        {
            // Show error.
            loginErrorLabel.InnerText = "The email and password combination you specified is not valid.";
            loginErrorLabel.Visible = true;
            return;
        }
    }
}
